/*

  MMetaverseSurface

  Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique
	
  http://www.maryjanesoft.com
  http://www.marlonj.com

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.
 
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  $Id$
	
*/

using TUIO;

namespace mjs.mmetaverse.surface
{
	/// <summary>
	/// Class to listen events produce by the reacTIVision application.
	/// </summary>
	public class SurfaceListener : TuioListener
	{
		/// <summary>
		/// The Metaverse Surface  
		/// </summary>		
		private MMetaverseSurface surface;

		/// <summary>
		/// The reacTIVision client to listen all events 
		/// </summary>
		private TuioClient tuioClient;

		/// <summary>
		/// Create a reacTIVision listener asociated with the surface 
		/// application that controls the avatars.
		/// </summary>
		/// <param name="surface">
		/// The surface application. <see cref="MMetaverseSurface"/>
		/// </param>
		public SurfaceListener(MMetaverseSurface surface) 
		{
			// Set the surface 
			this.surface = surface;

			// Create a new client a start to listen 
			tuioClient = new TuioClient();
			tuioClient.addTuioListener(this);
			tuioClient.connect();
		}

		/// <summary>
		/// Disconnect the TUIO client from the reacTIVision application 
		/// </summary>
		public void disconnect() 
		{
			tuioClient.disconnect();
		}		

		/// <summary>
		/// Call when a object touch the surface.
		/// </summary>
		/// <param name="o">
		/// Representation of the object. <see cref="TuioObject"/>
		/// </param>
		public void addTuioObject(TuioObject o)
		{
		}

		/// <summary>
		/// Call when a object updates his location or rotate.
		/// </summary>
		/// <param name="o">
		/// Representation of the object. <see cref="TuioObject"/>
		/// </param>
		public void updateTuioObject(TuioObject o)
		{
			// Rotate the entity asociated with the fiducial 
			surface.rotate(o.getFiducialID(),o.getAngle());
			
			// Move the entity acoording location of the fiducial 
			// The entity moves inside a region, and the region have 
			// dimensions 256x256, so scale the new position 
			surface.move(o.getFiducialID(),
			                     o.getX(),o.getY());
		}

		/// <summary>
		/// Call when a object leaves the surface.
		/// </summary>
		/// <param name="o">
		/// Representation of the object. <see cref="TuioObject"/>
		/// </param>
		public void removeTuioObject(TuioObject o)
		{
		}

		/// <summary>
		/// Call when a cursor appears.
		/// </summary>
		/// <param name="o">
		/// Representation of the cursor. <see cref="TuioObject"/>
		/// </param>
		public void addTuioCursor(TuioCursor c)
		{
		}

		/// <summary>
		/// Call when a cursor updates location.
		/// </summary>
		/// <param name="o">
		/// Representation of the cursor. <see cref="TuioObject"/>
		/// </param>
		public void updateTuioCursor(TuioCursor c)
		{
		}
		
		/// <summary>
		/// Call when a cursor is removed from the table.
		/// </summary>
		/// <param name="o">
		/// Representation of the cursor. <see cref="TuioObject"/>
		/// </param>		
		public void removeTuioCursor(TuioCursor c)
		{
 		}

		/// <summary>
		/// Call each time the reacTIVision can refresh the data. 
		/// </summary>
		/// <param name="timestamp">
		/// Time. <see cref="System.Int64"/>
		/// </param>
		public void refresh(long timestamp)
		{
		}
	}
}
